package com.fliad.ai.controller;

import cn.hutool.core.util.IdUtil;
import com.fliad.ai.dto.WorkFlowDto;
import com.fliad.ai.entity.AiWorkflow;
import com.fliad.ai.service.AiWorkflowService;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Result;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class AiWorkflowController {

    @Inject
    AiWorkflowService aiWorkflowService;

    // 创建
    @Post
    @Mapping("")
    public Result<Boolean> create(@Body AiWorkflow workflow) throws SQLException {
        workflow.setId(IdUtil.getSnowflakeNextId());
        workflow.setCreated(new Date());
        workflow.setModified(new Date());
        return Result.succeed(aiWorkflowService.create(workflow));
    }

    // 查询单个
    @Get
    @Mapping("/{id}")
    public Result<AiWorkflow> get(@Path("id") Long id) throws SQLException {
        return Result.succeed(aiWorkflowService.get(id));
    }

    // 查询列表
    @Get
    @Mapping("")
    public Result<List<AiWorkflow>> list() throws SQLException {
        return Result.succeed(aiWorkflowService.list());
    }

    // 更新
    @Put
    @Mapping("/{id}")
    public Result<Boolean> update(@Path("id") Long id, @Body AiWorkflow workflow) throws SQLException {
        workflow.setModified(new Date());
        return Result.succeed(aiWorkflowService.update(id, workflow));
    }

    @Post
    @Mapping("/save-flow")
    public Result<Boolean> saveFlow(WorkFlowDto workFlowDto) throws SQLException {
        AiWorkflow aiWorkflow = new AiWorkflow();
        aiWorkflow.setId(workFlowDto.getFlowId());
        aiWorkflow.setModified(new Date());
        aiWorkflow.setContent(workFlowDto.getData());
        return Result.succeed(aiWorkflowService.saveFlow(workFlowDto.getFlowId(), aiWorkflow));
    }

    // 删除
    @Delete
    @Mapping("/{id}")
    public Result<Boolean> delete(@Path("id") Long id) throws SQLException {
        return Result.succeed(aiWorkflowService.delete(id));
    }
}